var app = new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conditions: {
      start_time: "",
      end_time: '',
      phone: '',
      name: "",
    },
    empty: false, //没有数据
    page: 1, //当前页数
    pageSize: 20,
    total: 0,
    list: [],
    loadmore: false,
    nomore: false,
    drawer: false,
    isFilter: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.is_franchiser = options.is_franchiser || "";
    wx.showLoading();
    this.getUserList();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },


  reloadData() {
    this.setData({
      page: 1,
      list: [],
      nomore: false,
      loadmore: false,
    })
    wx.showLoading({
      mask: true,
    })
    this.getUserList()
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },
  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  inputContent(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getUserList();
    }
  },


  //获取用户列表
  getUserList() {
    const param = {
      "page": this.data.page,
      "pageSize": this.data.pageSize,
      "phone": this.data.conditions.phone,
      "name": this.data.conditions.name,
      "start_time": this.data.conditions.start_time,
      "end_time": this.data.conditions.end_time
    }
    if (this.is_franchiser) {
      param.type = 1;
    }
    app.ajax({
      url: "Partner/Users/getUserListTwo",
      data: param,
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code > 0) {
          const source = res.data.data.order;
          let list = this.data.list.concat(source);
          let total = res.data.data.count;
          this.setData({
            total,
            empty: total == 0 ? true : false,
            loadmore: list.length < total,
            nomore: list.length >= total,
            drawer: false,
            list
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        app.showToast("获取数据失败");
      }
    })
  },

  navWaterCard(e) {
    const user_id = e.currentTarget.dataset.user_id;
    wx.navigateTo({
      url: `waterCard?user_id=${user_id}${this.is_franchiser && "&is_franchiser=1"}`,
    })
  }


})